#!/bin/bash

time="$(date +%Y"-"%m"-"%d"_"%k":"%M":"%S)"

test -z "${1}" -o -z "${2}" && \
        echo "param ERROR!, tomcat7[8080]eg: sh install-tomcat.sh tomcat7 808" \
        &&  exit -1

tomcat_name=${1}
port=${2}
cd $(cd `dirname $0`; pwd)
echo ">>"`pwd`
tomcat_dir=`pwd`
tomcat_base="/usr/bx_app/program/$tomcat_name"

echo "$time begin to install--------------------------$tomcat_name"  
# java
export JAVA_HOME="/usr/java/jdk1.7.0_79"
java_home=`echo $JAVA_HOME`
echo "java home is $java_home"
test -z "$java_home" &&  \
        echo "ERROR! java_home is not set.exit" && exit -1

test ! -f "/usr/bin/unzip" && yum -y install unzip
test ! -f "/usr/bin/unzip" && echo "Error! unzip tools installed failed." && exit -1
# tomcat template
test ! -d "$tomcat_dir/tomcat7" && \
        echo "tomcat_dir:$tomcat_dir/tomcat7 is not exist.unzip now" \
        && unzip -q tomcat7.zip
test ! -d "$tomcat_dir/tomcat7" && \
        echo "ERROR! tomcat_dir:$tomcat_dir/tomcat7 is not exist.exit" \
        && exit -1;
echo "cp $tomcat_dir/tomcat7 --> $tomcat_base"
test ! -d "/usr/bx_app/program" && \
        mkdir -p /usr/bx_app/program
test -d "$tomcat_base" && \
        echo "ERROR! tomcat_base: $tomcat_base is already exist!" \
        && exit -1;
cp -r "$tomcat_dir/tomcat7" "$tomcat_base"

cd $tomcat_base/bin/
echo ">>" `pwd`
echo "mv jsvc_tomcat7xxx --> $tomcat_name"
mv jsvc_tomcat7xxx $tomcat_name
echo "copy & config service file..."
cp $tomcat_dir/tomcat7xxx /etc/init.d/$tomcat_name
echo "modify /etc/init.d/$tomcat_name 's param jsvc_tomcat7xxx --> $tomcat_name."
sed -i "s/jsvc_tomcat7xxx/$tomcat_name/g" /etc/init.d/$tomcat_name
echo "modify /etc/init.d/$tomcat_name 's param tomcat7xxx --> $tomcat_name."
sed -i "s/tomcat7xxx/$tomcat_name/g" /etc/init.d/$tomcat_name
echo "modify $tomcat_base/conf/logging.properties 's param tomcat7xxx --> $tomcat_name."
sed -i "s/tomcat7xxx/$tomcat_name/g" $tomcat_base/conf/logging.properties
echo "modify $tomcat_base/conf/server.xml 's param tomcat7xxx --> $tomcat_name."
sed -i "s/tomcat7xxx/$tomcat_name/g" $tomcat_base/conf/server.xml
echo "modify $tomcat_base/conf/server.xml 's param {port} --> $port."
sed -i "s/{port}/$port/g" $tomcat_base/conf/server.xml

BX_LOG=/var/log
echo "BX_LOG=/var/log"
tomcat_log_dir="$BX_LOG/logs/$tomcat_name"
if [ ! -d "$tomcat_log_dir" ]; then
        mkdir -p "$tomcat_log_dir"
fi
tomcat_audit_dir="$BX_LOG/audit/$tomcat_name"
if [ ! -d "$tomcat_audit_dir" ]; then
        mkdir -p "$tomcat_audit_dir"
fi
if [ -z `cat /etc/group|grep tomcat` ]; then
        echo user tomcat is not exist, add now.
        useradd tomcat
else
        echo user tomcat is already added.
fi
echo "chmod log path:$tomcat_log_dir & $tomcat_audit_dir."
chmod +x /etc/init.d/*
chown -R tomcat:tomcat "$BX_LOG/logs"
chmod -R 755 "$BX_LOG/logs"
chown -R tomcat:tomcat "$tomcat_audit_dir"
chown tomcat:tomcat "$BX_LOG/audit"
chmod -R 755 "$BX_LOG/audit"
chown -R tomcat:tomcat $tomcat_base
chmod -R 755 $tomcat_base
rm -rf $tomcat_base/webapps/*
echo " install $tomcat_name end"  
